package com.zh.device.controller;

import com.alibaba.fastjson.JSONObject;

import com.zh.analysis.service.GpsService;
import com.zh.common.utils.R;

import com.zh.common.websocket.ThmsWebSocketHandler;
import com.zh.common.websocket.ThmsWebSocketMessage;

import com.zh.device.dao.SiteDao;
import com.zh.device.service.DeviceService;
import com.zh.analysis.service.MattressService;
import com.zh.equipment.domain.BindingRelaDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 硬件设备post接入
 */
@Controller
@RequestMapping("/device")
public class DeviceController {

    private final Logger logger = LoggerFactory.getLogger(DeviceController.class);


    @Autowired
    private DeviceService deviceService;

    @Autowired
    private MattressService mattressService;

    @GetMapping()
    String deviceBinding(){
        return "device/test";
    }


    /**
     * 硬件设备post
     * @param jsonParam
     * @return
     */
    @RequestMapping(value = "postData", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public R postData(@RequestBody JSONObject jsonParam) throws Exception{
        logger.info("DeviceController postData:" + jsonParam);
        deviceService.deviceProsson(jsonParam);
        System.out.println("DeviceController postData:" + jsonParam);
        R.ok();

        return R.ok();
    }


    /**
     * websocket推送给web页面
     *
     * @param jsonObject
     */
    private void webSocket(JSONObject jsonObject) {
        //建立连接
        ThmsWebSocketHandler swsh = ThmsWebSocketHandler.getSystemWebSocketHandler();
        //发送的消息
        try{
           // jsonObject.put("createTime", DeviceUtil.formatCreateDate(jsonObject.getString("timeCreate")) );
        }catch (Exception e){

        }
        ThmsWebSocketMessage twsm = new ThmsWebSocketMessage("SING", "familyuser", "sign", jsonObject);
        //发送
        swsh.sendMessageToUsers(twsm);
    }


    /**
     * 床垫数据
     * @param jsonParam
     * @return
     */
    @RequestMapping(value = "mattress", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public R mattress(@RequestBody JSONObject jsonParam) {

        logger.info("DeviceController mattress:" + jsonParam);
        mattressService.proxyMattress(jsonParam);

        return R.ok();
    }

    @Autowired
    private GpsService gpsService;
    /**
     * gps
     * @param jsonParam
     * @return
     */
    @RequestMapping(value = "gps", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public R gps(@RequestBody JSONObject jsonParam) {

        logger.info("DeviceController gps:" + jsonParam);
        gpsService.gps(jsonParam);
        System.out.println("DeviceController gps:" + jsonParam);
        R.ok();

        return R.ok();
    }

    @Autowired
    private SiteDao siteDao;
    /**
     * 床垫数据
     * @param jsonParam
     * @return
     */
    @RequestMapping(value = "getAllBindList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<BindingRelaDTO> getAllBindList(@RequestBody JSONObject jsonParam) {

        List<BindingRelaDTO> bindingRelaDTOS=new ArrayList<>();

        BindingRelaDTO bindingRelaDTO1=new BindingRelaDTO();
        bindingRelaDTO1.setBidingTypes(0);
        bindingRelaDTO1.setBindingDTOList(siteDao.findBindingDTO1());
        bindingRelaDTOS.add(bindingRelaDTO1);

        BindingRelaDTO bindingRelaDTO2=new BindingRelaDTO();
        bindingRelaDTO2.setBidingTypes(1);
        bindingRelaDTO2.setBindingDTOList(siteDao.findBindingDTO2());
        bindingRelaDTOS.add(bindingRelaDTO2);

        BindingRelaDTO bindingRelaDTO3=new BindingRelaDTO();
        bindingRelaDTO3.setBidingTypes(2);
        bindingRelaDTO3.setBindingDTOList(siteDao.findBindingDTO3());
        bindingRelaDTOS.add(bindingRelaDTO3);


        BindingRelaDTO bindingRelaDTO4=new BindingRelaDTO();
        bindingRelaDTO4.setBidingTypes(3);
        bindingRelaDTO4.setBindingDTOList(siteDao.findBindingDTO4());
        bindingRelaDTOS.add(bindingRelaDTO4);

        return bindingRelaDTOS;
    }


}
